﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace PictureTransform
{
    public partial class ProgressForm : Form
    {
        private int FileCount;
        private AutoResetEvent StopWorker;

        public ProgressForm(int fileCount, AutoResetEvent stopWorker)
        {
            InitializeComponent();

            StopWorker = stopWorker;
            FileCount = fileCount;
            progressBar.Minimum = 0;
            progressBar.Maximum = FileCount;
            progressBar.Step = 1;
            lbProgress.Text = "0 of " + FileCount.ToString();
        }

        public void PerformStep(string file)
        {
            progressBar.PerformStep();
            lbProgress.Text = progressBar.Value.ToString() +" of " + progressBar.Maximum.ToString();
            if (progressBar.Value == progressBar.Maximum)
                Close();
        }

        private void ProgressForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (progressBar.Value >= progressBar.Maximum)
            {
                e.Cancel = false;
                return;
            }

            e.Cancel = true;

            if( MessageBox.Show("Are you sure you want to cancel auto-rotate?",
                                "Confirm closing", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                if( StopWorker != null )
                    StopWorker.Set();
                e.Cancel = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
